<% // Copyright 2008 Carnegie Mellon University
// Licensed under the Apache License, Version 2.0 (the "License"); 
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//  
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, 
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License. %>
<%@page language="java" contentType="text/javascript; charset=UTF-8" %>
<%@page pageEncoding="UTF-8" %>
<%@page import="java.util.Properties"%>
<%@page import="java.io.PrintWriter"%>
<%@page import="java.io.StringWriter"%>
<%@page import="edu.cmu.lti.sepia.util.Logger"%>
<%@page import="edu.cmu.lti.sepia.data.resource.ResourceManager"%>
<%@page import="edu.cmu.lti.sepia.data.resource.MessageResource"%>
<%@page import="edu.cmu.lti.sepia.data.session.SessionManager"%>
<%@page import="edu.cmu.lti.sepia.data.db.SQL"%>
<%@page import="edu.cmu.lti.sepia.util.ServletUtil"%>
<%@page import="edu.cmu.lti.sepia.infra.system.model.Permission"%>
<%@page import="edu.cmu.lti.sepia.data.db.PermissionDAO"%>
<% 
request.setCharacterEncoding("UTF-8");

// Local variables (JSP)
String userName = request.getRemoteUser();

if ( userName == null ) {
  out.println( "ERROR: userName is null (session lost?)." );
} else {
  out.println( "// session is still alive" );
}

SQL sql = new SQL();

//One time process after logging in
if ( session.getAttribute("userId")==null ) {
  SessionManager.storeUser( sql, userName, session );
  SessionManager.storeHost( request, session );
  Logger.log( sql, session, "LOG IN", "" );
}

//One time process after task has been selected
String tns = request.getParameter("taskNameShort");
if ( tns!=null ) {
  if ( tns.equals("none") ) {
    SessionManager.resetTask( session );
  } else {
    SessionManager.storeTask(sql, tns, session);
  }
}

ServletUtil s = new ServletUtil( request );
int userId = s.readInt("userId",-1);
String userRole = s.readString("userRole", "user");
Boolean isAdmin = (Boolean)session.getAttribute("isAdmin");
if (isAdmin==null) isAdmin = false;

int taskId = s.readInt("taskId",-1);
Permission permission = null;
if ( userId>0 ) {
  permission = PermissionDAO.findByUserAndTaskId(sql,userId,taskId);
}
sql.close();
String taskName = s.readString("taskName","");
String taskNameShort = s.readString("taskNameShort", "");
String srcLang = s.readString("srcLang", "");
String trgLang = s.readString("trgLang", "");
boolean isMonolingual = s.readBoolean("isMonolingual", false);
String[] types = (String[])session.getAttribute("types");
String[] tdLabels = (String[])session.getAttribute("tdLabels");
String[] irLabels = (String[])session.getAttribute("irLabels");

String lang = request.getParameter("displayLang");
if ( request.getParameter("displayLang")!=null ) {
  lang = request.getParameter("displayLang");
  session.setAttribute("displayLang",lang);
} else {
  lang = s.readString("displayLang","EN");
}
// This code is needed for the blank displayLang...
if (lang==null || lang.length()==0) {
  lang = "EN";
}
%>
// Shared variables (JS) 
var userName = "<%=userName%>";
var userRole = "<%=userRole%>";
var userId = "<%=userId%>";
var lang = "<%=lang%>";
var taskName = "<%=taskName%>";
var taskNameShort = "<%=taskNameShort%>";
var isMonolingual = <%=isMonolingual?1:0%>;
var srcLang = "<%=srcLang%>";
var trgLang = "<%=trgLang%>";
var contextPath = "<%=request.getContextPath()%>";

var isAdmin = <%=isAdmin?"1":"0"%>;
var privilege = <%=(permission!=null?permission.getPrivilege():"0")%>;
<%

if ( types != null ) {
  out.print("var types = new Array(");
  for ( int i=0; i<types.length; i++ ) {
    out.print( (i>0?", ":"")+"\""+types[i]+"\"" );
  }
  out.println(");");
}

if ( tdLabels != null ) {
  out.print("var tdLabels = new Array(");
  for ( int i=0; i<tdLabels.length; i++ ) {
    out.print( (i>0?", ":"")+"\""+tdLabels[i]+"\"" );
  }
  out.println(");");
}
if ( irLabels != null ) {
  out.print("var irLabels = new Array(");
  for ( int i=0; i<irLabels.length; i++ ) {
    out.print( (i>0?", ":"")+"\""+irLabels[i]+"\"" );
  }
  out.println(");");
}
%>

<%
//SEPIA version read from src/main/config/config.properties
Properties p = ResourceManager.readBundledConfig();
//e.g. v1.01
String sepiaVersion = "v"+p.getProperty("version","");
%>
var sepiaVersion = "<%=sepiaVersion%>";

// JSP dumps message resources (var messageResource = new Array();)
var messageResource = new Array();
<%
String[] langs = {"CS","CT","EN","JA"};
for ( String lng : langs ) {
  out.println( MessageResource.messageResourceToJs( lng, "messageResource" ) );
}
%>

var msg_answer_text = "<%=MessageResource.getString(lang, "answer_text")%>";
var msg_answer_nugget = "<%=MessageResource.getString(lang, "answer_nugget")%>";
var msg_similar_nugget = "<%=MessageResource.getString(lang, "similar_nugget")%>";
var msg_doc_id = "<%=MessageResource.getString(lang, "doc_id")%>";
var msg_delete = "<%=MessageResource.getString(lang, "delete")%>";
var msg_add = "<%=MessageResource.getString(lang, "add")%>";
var trg_lang = "<%=MessageResource.getString(lang, "trg_lang")%>";

var msg_failed = "<%=MessageResource.getString(lang, "msg_failed")%>";
var msg_error = "<%=MessageResource.getString(lang, "msg_error")%>";

var msg_done_task = "<%=MessageResource.getString(lang, "msg_done_task")%>";

var msg_suc_q_add = "<%=MessageResource.getString(lang, "msg_suc_q_add")%>";
var msg_suc_q_save = "<%=MessageResource.getString(lang, "msg_suc_q_save")%>";
var msg_suc_q_del = "<%=MessageResource.getString(lang, "msg_suc_q_del")%>";

var msg_suc_ans_add = "<%=MessageResource.getString(lang, "msg_suc_ans_add")%>";
var msg_suc_ans_del = "<%=MessageResource.getString(lang, "msg_suc_ans_del")%>";

var msg_suc_nug_add = "<%=MessageResource.getString(lang, "msg_suc_nug_add")%>";
var msg_suc_nug_del = "<%=MessageResource.getString(lang, "msg_suc_nug_del")%>";

var msg_suc_nug_save = "<%=MessageResource.getString(lang, "msg_suc_nug_save")%>";
var msg_suc_updated  = "<%=MessageResource.getString(lang, "msg_suc_updated")%>";

var msg_suc_ireval_save = "Saved IR Judgment result";

var msg_warn_docid = "ERROR: Document ID is empty";

var done_prefix = "[x] ";

function msg( language, key ) {
  switch (arguments.length) {
    case 1: return messageResource[ key ];break;
    case 2: return messageResource[ language+"."+key ];break;
    default: return ''; break;
  }
}
